﻿Imports System.Data.OleDb
Imports System.Net.Mail
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports System.Net
Module AlertasEmail

    Public Function getEmailDestino(ByVal nombreEmpresa As String)
        Using cadenaConexBD()
            Dim getEmail As New OleDbCommand("SELECT email_emp FROM empresa WHERE nomb_empr = '" & nombreEmpresa & "'", cadenaConexBD)
            Dim email = getEmail.ExecuteScalar
            Return email
        End Using
    End Function

    Public Sub enviarAlerta(ByVal correoDestino As String, ByVal Asesor As String, ByVal Auditado As String, ByVal Caso As String, ByVal Lote As String, ByVal fecha_llam As String, ByVal Puntaje As String, ByVal encontra As String)

        'Obtengo los datos de configuracion para el envio
        Dim infoConfig As New C_config
        Dim drConfig As OleDbDataReader
        Dim Encabezado As String = ""
        Dim Firma As String = ""
        Dim Email As String = ""
        Dim Password As String = ""
        Dim SmtpHost As String = ""
        Dim Port As String = ""

        drConfig = infoConfig.GetConfig
        While drConfig.Read
            'Cambio el cursor para indicar que se esta trabajando
            Cursor.Current = Cursors.WaitCursor

            If Not drConfig.IsDBNull(1) Then
                Encabezado = drConfig.GetString(1)
            End If
            If Not drConfig.IsDBNull(2) Then
                Firma = drConfig.GetString(2)
            End If
            If Not drConfig.IsDBNull(3) Then
                Email = drConfig.GetString(3)
            End If
            If Not drConfig.IsDBNull(4) Then
                Password = drConfig.GetString(4)
            End If
            If Not drConfig.IsDBNull(5) Then
                SmtpHost = drConfig.GetString(5)
            End If
            If Not drConfig.IsDBNull(6) Then
                Port = drConfig.GetString(6)
            End If
        End While
        drConfig.Close()
        cadenaConexBD.Close()

        Dim correo As New MailMessage
        Dim smtp As New SmtpClient()


        Try
            correo.From = New MailAddress(Email, "Calidad VF & Asociados", System.Text.Encoding.UTF8)


            correo.To.Add(correoDestino)
            correo.SubjectEncoding = System.Text.Encoding.UTF8
            correo.Subject = "Alerta de Auditoria"
            correo.Body = "<html><body><br/>" + Encabezado + "<br/><br/>" + "Asesor: " + Asesor + "<br/>" + "Auditado: " + Auditado + "<br/>" + "Caso: " + Caso + "<br/>" + "Lote Nº: " + Lote + "<br/>" + "Fecha de llamada: " + fecha_llam + "<br/>" + "Puntaje: " + Puntaje + "<br/><br/>" + "Aspectos a Trabajar:" + "<br/>" + encontra + "<br/><br/>" + "Quedamos a vuestra disposición ante cualquier inquietud." + "<br/><br/>" + Firma + "</body></html>"
            correo.BodyEncoding = System.Text.Encoding.UTF8
            correo.IsBodyHtml = True '(formato tipo web o normal: true = web)
            correo.Priority = MailPriority.High '>> prioridad

            smtp.Credentials = New System.Net.NetworkCredential(Email, Password)
            smtp.Port = Port
            smtp.Host = SmtpHost
            smtp.EnableSsl = True
            ServicePointManager.ServerCertificateValidationCallback = Function(s As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True
            smtp.Send(correo)
        Catch exeption As Exception
            MessageBox.Show(exeption.Message, "Error", MessageBoxButtons.OK)
            Exit Sub
        End Try
        MsgBox("Alerta enviada con éxito", MsgBoxStyle.Information, "Alerta")

    End Sub

End Module
